package com.example.travel_front.Fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import com.example.travel_front.Adapter.NursingHomeAdapter;
import com.example.travel_front.Model.NursingHome;
import com.example.travel_front.R;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends Fragment {

    public HomeFragment() {

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_nursinghome, container, false);

        // 初始化RecyclerView和NursingHomeAdapter
        RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
        List<NursingHome> nursingHomeList = displayList(); // 从数据库或API获取数据
        NursingHomeAdapter adapter = new NursingHomeAdapter(getContext(), nursingHomeList);
        // 设置布局管理器和适配器
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(adapter);
        // Initialize spinners
        Spinner spinnerDistance = view.findViewById(R.id.spinnerDistance);
        Spinner spinnerPrice = view.findViewById(R.id.spinnerPrice);
        Spinner spinnerScore = view.findViewById(R.id.spinnerScore);

        // Set up the adapters for each spinner with example data
        setupSpinner(spinnerDistance, new String[]{"1 km", "5 km", "10 km"});
        setupSpinner(spinnerPrice, new String[]{"$100-500", "$500-1000", "$1000+"});
        setupSpinner(spinnerScore, new String[]{"1.0", "2.0", "3.0"});


        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }


    private List<NursingHome> getNursingHomeList() {
        // TODO: 实现从数据库或API获取护理之家列表的逻辑
        return new ArrayList<>(); // 返回空列表或从数据源获取的列表
    }
    List<NursingHome> displayList() {
        List<NursingHome> list = new ArrayList<>();

        list.add(new NursingHome(
                "https://jdhn.oss-cn-hangzhou.aliyuncs.com/WechatIMG530.jpg", // 在实际应用中这里应该是图片的URL
                "Sunshine Elderly Care",
                "1234 Main St, YourCity",
                "$2000 - $4000 per month",
                5.0f ,// 评分为5.0星
                1
        ));

        list.add(new NursingHome(
                "https://jdhn.oss-cn-hangzhou.aliyuncs.com/WechatIMG531.jpg",
                "Happy Haven",
                "4321 King's Road, YourCity",
                "$2500 - $4500 per month",
                4.0f, // 评分为4.0星
                2
        ));

        list.add(new NursingHome(
                "https://jdhn.oss-cn-hangzhou.aliyuncs.com/WechatIMG532.jpg",
                "Tranquil Retirement Home",
                "789 Peaceful Ln, YourCity",
                "$3000 - $5000 per month",
                3.0f ,// 评分为3.0星
                3
        ));
        list.add(new NursingHome(
                "https://jdhn.oss-cn-hangzhou.aliyuncs.com/WechatIMG533.jpg",
                "Tranquil Retirement Home",
                "789 Peaceful Ln, YourCity",
                "$3000 - $5000 per month",
                3.0f ,// 评分为3.0星
                4
        ));
        list.add(new NursingHome(
                "https://jdhn.oss-cn-hangzhou.aliyuncs.com/WechatIMG534.jpg",
                "Tranquil Retirement Home",
                "789 Peaceful Ln, YourCity",
                "$3000 - $5000 per month",
                3.0f ,// 评分为3.0星
                5
        ));
        return list;
    }
    private void setupSpinner(Spinner spinner, String[] items) {
        ArrayAdapter<String> adapter = new ArrayAdapter<>(requireActivity(),
                android.R.layout.simple_spinner_item, items);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
    }

}

